import { RtcDefaultApi } from '@/unit/axios';
import { defaultErrorHandler } from '@/unit/error-handler';
import { NavigateTo, RedirectTo } from '@/unit/navigate-to';
import { action, makeObservable, observable } from 'mobx';
import { singleton } from 'tsyringe';
import { Live } from './types';

@singleton()
export class Home {
  @observable public List: Live[] = [];

  @observable public activekey = 0;

  @action public async getList() {
    const params = '';
    try {
      const result = await RtcDefaultApi.apiLiveAllPost(params);

      if (result) {
        this.setList(result);
      }
    } catch (error) {
      defaultErrorHandler(error);
    }
  }

  @action private setList(value: Live[]) {
    this.List = value;
  }

  @action public setState = (state: number) => {
    switch (state) {
      case 0:
        RedirectTo('home');
        break;
      case 2:
        RedirectTo('mine');
        break;
      // default:
      //   RedirectTo('home');
    }
    this.activekey = state;
  };

  constructor() {
    makeObservable(this);
  }
}
